package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Course;
import cn.edu.ctbu.lddweb.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 课程管理Controller
 */
@Controller
public class CourseController {
    @Autowired
    private CourseService courseService;

    //后台管理系统
    @RequestMapping("/course/index")
    public String index(Model model, ModelMap map,Course course){
        List<Course> courses = courseService.selectList(course);
        model.addAttribute("course",courses);
        return "/course/index";
    }

    //搜索/重置
    @RequestMapping("/selectCourse")
    public String selectCourse(@RequestParam(value = "courseName",required = false) String courseName, Model model){
        Course course = new Course();
        course.setCourseName(courseName);
        List<Course> list = courseService.selectList(course);
        model.addAttribute("course",list);
        return "/course/index";
    }

    //新增页面
    @RequestMapping("/course/insert")
    public String insert(){
        return "/course/insert";
    }

    //新增操作
    @RequestMapping("/insertCourse")
    public String insertCourse(Course course){
        //课程不能重复
        List<Course> list = courseService.selectList(course);
        if(list.size()!=0){
            return "error111";
        }
        courseService.insertCourse(course);
        return "/course/index";
    }


    //修改
    @RequestMapping("/course/edit")
    public String edit(int id,Model model){
        Course course = courseService.findID(id);
        model.addAttribute("course",course);
        return "/course/edit";
    }

    //修改操作
    @RequestMapping("/updateCourse")
    public String updateCourse(Course course){
        //判断修改的课程名称是否已经存在
        Boolean boo = courseService.checkSupplierUnique(course);
        if(boo){
            return "error111";
        }
        courseService.updateCourse(course);
        return "/course/index";
    }

    //删除
    @RequestMapping("/deleteCourse")
    public String deleteCourse(int id){
        courseService.deleteCourse(String.valueOf(id));
        return "/course/index";
    }
}
